﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ecopsy.DataTier;
using AgencyEntityLib.Context  ;
using System.Threading;
using System.Data.SqlClient;

namespace AgencyEntityLib.BaseEntity
{
    public class BaseTask : SqlAgencyEntity, ICloneable  
    {

        

        [EntityNonSerialized]
        protected override string TagName
        {
            get
            {
                return "Task";
            }
        }
         
        private DateTime _dtFinish = Context.AgencyLib_GlobalConstants.MinDate  ;
        public DateTime dtFinish { get { return _dtFinish; } set { _dtFinish = value; } }

        private DateTime _dtAlertDate = Context.AgencyLib_GlobalConstants.MaxDate;
        public DateTime dtAlertDate { get { return _dtAlertDate; } set { _dtAlertDate = value; } }

        private bool _IsClosed = false;
        /// <summary>
        /// Задача закрыта
        /// </summary>
        public bool IsClosed { get { return _IsClosed; } set { _IsClosed = value; } }

        private TreeDictionaryItemValue _TypeValue = null;
        /// <summary>
        /// Тип задачи (письмо звонок и т.д.)
        /// </summary>
        public TreeDictionaryItemValue TypeValue
        {
            get { return _TypeValue; }
            set { _TypeValue = value; }
        }

        private string  _ClientID =string.Empty ;        
        public string  ClientID
        {
            get { return _ClientID; }
            set { _ClientID = value; }
        }

        private string _EmplID = string.Empty;
        
        /// <summary>
        /// Ответсвенный за задачу
        /// </summary>
        public string EmplID
        {

            get { return _EmplID; }
            set { _EmplID = value; }
        }

        private string _TaskContent = string.Empty;
        /// <summary>
        /// Содержание задачи
        /// </summary>
        public string TaskContent
        {
            get { return _TaskContent; }
            set { _TaskContent = value; }
        }


        private  EntityList _History = new EntityList ();
        /// <summary>
        /// История изменения задачи 
        /// </summary>
        public EntityList History { get { return _History; } set { _History = value; } }


        public bool IsCacheObject
        {
            get { return true; }


        }

        #region ICloneable Members

        public object Clone()
        {
            BaseTask t = new BaseTask();
            t.ClientID = this.ClientID;
            t.dtFinish = this.dtFinish;
            t.EmplID = this.EmplID;
            t.IsClosed = this.IsClosed;
            t.TaskContent = this.TaskContent;
            t.TypeValue = this.TypeValue;            
            return t;
        }

        #endregion


      




       

        /// <summary>
        /// Если задача на текущий момент открыта а дата окончания меньше текущей даты - задача просроченна
        /// </summary>
        /// <param name="_Task"></param>
        /// <returns></returns>
        public  string GetTaskStatusName()
        {
            if (!IsClosed && dtFinish < DateTime.Now) return "Просроченна";

            return (IsClosed ? "Закрыта" : "Открыта");
        }

        public  Employeer GetEmployeer()
        {
            if (string.Empty == EmplID) return null;
            wsFace ws = (wsFace)AgencyLib_GlobalConstants.CurrentAgency.WS_FactoryList[WS_FactoryList.wsKey_Face];
            return ws.GetEmpl(EmplID);
        }
    }

    public class Task : BaseTask, ISqlSignature
    {
        private static string _xml = string.Empty;
        private static string _proc = string.Empty;
        private static string _sc = string.Empty;
        private static string _id = string.Empty;
        private static string _AgencyId = string.Empty;
        private static List<SqlParameter> _AddParametrs = new List<SqlParameter>();


        public void AsynSave()
        {

            // асинхронное сохранение
            _xml = this.ToXml(false);
            _id = this.ID;
            _sc = AppContext.GetSC().ConnectionString;
            _AgencyId = this.AgencyID;
            _proc = ((ISqlSignature)this).SaveProcedure;
            SqlParameter p = new SqlParameter("DtFinish", System.Data.SqlDbType.DateTime);
            p.Value = dtFinish;
            _AddParametrs.Clear();
            _AddParametrs.Add(p);
            Thread t = new Thread(new ThreadStart(FastSave));
            t.Start();
            this.SetHash();
        }

        void FastSave()
        {
            //SqlTools.FastSave(_xml, _id, _AgencyId, _proc, _sc, _AddParametrs);
            SqlTools.FastSave(_xml); 
        }

        #region ISqlSignature Members

        public string SaveProcedure
        {
            get { return "TaskSave"; }
        }

        public string DeleteProcedure
        {
            get { return "TaskDelete"; }
        }

        public string LoadProcedure
        {
            get { return "TaskLoad"; }

        }

        #endregion
    }

    /// <summary>
    /// Запись в жулнале истории изменений задачи 
    /// </summary>
    public class TaskHistoryItem:EntitySingle 
    {
        public BaseTask Task { get; set; }
        public Employeer Creator { get; set; }
        public DateTime Date { get; set; }


      

        
    }
   
}
